<?php

 
Zend_Loader::loadClass("BaseDBRows");
Zend_Loader::loadClass("TankInspection");
class TankInspections extends BaseDBRows
{
	protected function setUp()
	{
		$this->_tableName = TankInspection::_tablename;
	}	
	
	public function getTankInspectionsByOrgId($orgId,$tankId="",$result="",$dateFrom="",$dateTo="",$system_name="") {

		Zend_Loader::loadClass("Tank");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("ti.*, t.name tank_name, t.system_name system_name");
		$query->addFrom($this->_tableName . " ti ");
		$query->addJoin("," . Tank::_tablename . " t ");
		$query->addWhere("ti.tank_id = t.tank_id");
		$query->addWhere("t.org_id = " . $orgId);
		$query->addOrderBy("t.name, ti.date_inspected desc");

		// system name
		if(strlen($system_name) > 0) {
			$query->addWhere("t.system_name = '" . $system_name."'");
		}			
		// tank
		if(strlen($tankId) > 0) {
			$query->addWhere("t.tank_id = " . $tankId);
		}
		// result
		if(strlen($result) > 0) {
			$query->addWhere("ti.result = '" . $result . "'");
		}
			
		// dates from - to
		if(strlen($dateFrom) > 0 && strlen($dateTo) > 0) {
			$query->addWhere(" ti.date_inspected between least('" . $dateFrom . "','" . $dateTo . "') and greatest('" . $dateFrom . "','" . $dateTo . "')" );	
		}
		elseif(strlen($dateFrom) > 0) {
			$query->addWhere(" ti.date_inspected >= '" . $dateFrom . "'" );
		}
		elseif(strlen($dateTo) > 0) {
			$query->addWhere(" ti.date_inspected <= '" . $dateTo . "'");
		}
		
		$db = DBCon::instance();
		// echo  $query->createSQL();die();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	
	
	
}
?>